﻿define(function (require, exports, module) {
    var Canvas = require('util/canvas');
    var ImageStorage = require('util/imageStorage');

    exports.loadAsync = eval(Wind.compile('async', function (data) {
        Canvas.get('#entering')[0].hide();
        for (var y = 0; y < 3; y++) {
            for (var x = 0; x < 3; x++) {
                var index = x + y * 3;
                var mapImage = getAreaImageBy(index);
                if (data.srcs[index]) {
                    var img = $await(ImageStorage.getAsync(data.srcs[index]));
                    mapImage.setImage(img);
                }
                mapImage.setAttrs({
                    x: mapImage.getX() - data.x, y: mapImage.getY() - data.y
                }).on('mousemove', function (e) {
                    var p = { x: e.clientX, y: e.clientY };
                    var range = calculatePointRange(p);
                    Canvas.get('#pointer')[0].setAttrs({
                        x: (Math.floor(p / m_rangeWidth) + range[0]) * m_rangeWidth,
                        y: (Math.floor(p / m_rangeHeight) + range[1]) * m_rangeHeight
                    });
                });
            }
        }
        Canvas.get('#mapLayer')[0].draw();
    }));

    function getAreaImageBy(index) {
        return Canvas.get(['#area', index].join('_'))[0];
    };

    var m_range = {
        topLeft: [-0.5, -0.5],
        topRight: [0.5, -0.5],
        middle: [0, 0],
        bottomLeft: [-0.5, 0.5],
        bottomRight: [0.5, 0.5]
    };

    var m_rangeWidth = 64, m_rangeHeight = 48;
    function calculatePointRange(p) {
        var rx = Math.floor(x / m_rangeWidth), ry = Math.floor(y / m_rangeHeight);
        var left = x % m_rangeWidth, top = y % m_rangeHeight;

        var topLeft = { x: rx * m_rangeWidth, y: ry * m_rangeHeight };
        var topCenter = { x: topLeft.x + m_rangeWidth / 2, y: topLeft.y };
        var topRight = { x: topLeft.x + m_rangeWidth, y: topLeft.y };
        var middleLeft = { x: topLeft.x, y: topLeft.y + m_rangeHeight / 2 };
        var middleRight = { x: topRight.x, y: middleLeft.y };
        var bottomLeft = { x: topLeft.x, y: topLeft.y + m_rangeHeight };
        var bottomCenter = { x: topCenter.x, y: bottomLeft.y };
        var bottomRight = { x: topRight.x, y: bottomLeft.y };

        if (pointInPolygon(p, [topCenter, middleRight, bottomCenter, middleLeft])) {
            //是否在中间菱形区域
            return m_range.middle;
        }
        else if (pointInPolygon(p, [topLeft, topCenter, middleLeft])) {
            //位于左上角三角区域内
            return m_range.topLeft;
        }
        else if (pointInPolygon(p, [topCenter, topRight, middleRight])) {
            //位于右上三角区域内
            return m_range.topRight;
        }
        else if (pointInPolygon(p, [middleLeft, bottomCenter, bottomLeft])) {
            //位于左下角三角区域内
            return m_range.bottomLeft;
        }
        else {
            //位于右下角三角区域内
            return m_range.bottomRight;
        }
    }

    //p: {x: 0, y: 0} arr: [{x: 0, y: 0}, {x: 0, y: 0}, {x: 0, y: 0},...]
    function pointInPolygon(p, arr) {
        var count = 0;
        var p1, p2;
        //(p.y - p1.y) * (p1.x - p2.x) / (p1.y - p2.y) + p1.x
        for (var i = 0, j = arr.length - 1; i < arr.length; j = i++) {
            p1 = arr[i];
            p2 = arr[j];

            if (p1.y = p2.y || p.y < Math.min(p1.y, p2.y) || p.y >= Math.max(p1.y, p2.y)) continue;

            var x = (p.y - p1.y) * (p1.x - p2.x) / (p1.y - p2.y) + p1.x;
            (x > p.x) && count++;

            return count % 2 == 1;
        }
        return count % 2 == 1;
    };
});